<template>
  <div class="login-modal-overlay" @click="closeModal">
    <div class="login-modal" @click.stop>
      <button class="close-btn" @click="closeModal">×</button>

      <h2>用户登录</h2>

      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label>用户名:</label>
          <input
            v-model="credentials.username"
            type="text"
            placeholder="请输入用户名"
            required
          >
        </div>

        <div class="form-group">
          <label>密码:</label>
          <input
            v-model="credentials.password"
            type="password"
            placeholder="请输入密码"
            required
          >
        </div>

        <button type="submit" :disabled="loading" class="login-btn">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>

      <div class="demo-accounts">
        <h4>测试账户：</h4>
        <div class="account-list">
          <div v-for="account in demoAccounts" :key="account.username"
               class="account-item" @click="fillDemoAccount(account)">
            {{ account.role }}: {{ account.username }} / {{ account.password }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { childEnglishApi } from '@/api/childEnglishApi';

export default {
  name: 'LoginModal',
  emits: ['close', 'login-success'],
  data() {
    return {
      credentials: {
        username: '',
        password: ''
      },
      loading: false,
      demoAccounts: [
        { username: 'admin', password: '123456', role: '管理员' },
        { username: 'parent_zhang', password: '123456', role: '家长' },
        { username: 'child_xiaoming', password: '123456', role: '幼儿' },
        { username: 'teacher_li', password: '123456', role: '教师' }
      ]
    };
  },
  methods: {
    closeModal() {
      this.$emit('close');
    },

    fillDemoAccount(account) {
      this.credentials.username = account.username;
      this.credentials.password = account.password;
    },

    async handleLogin() {
      this.loading = true;
      try {
        const response = await childEnglishApi.login(this.credentials);
        if (response.code === 200) {
          // 保存用户信息和token
          localStorage.setItem('user', JSON.stringify(response.data.user));
          localStorage.setItem('token', response.data.token);

          this.$emit('login-success', response.data.user);
          this.closeModal();
        } else {
          alert('登录失败: ' + response.message);
        }
      } catch (error) {
        alert('登录失败: ' + (error.response?.data?.message || error.message));
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.login-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.login-modal {
  background: white;
  padding: 30px;
  border-radius: 15px;
  width: 400px;
  max-width: 90%;
  position: relative;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.close-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #333;
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-weight: bold;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  border-color: #3498db;
  outline: none;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.login-btn:hover:not(:disabled) {
  background: #2980b9;
}

.login-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.demo-accounts {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.demo-accounts h4 {
  color: #7f8c8d;
  margin-bottom: 10px;
  font-size: 14px;
}

.account-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.account-item {
  padding: 8px 12px;
  background: #f8f9fa;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.3s;
}

.account-item:hover {
  background: #e9ecef;
}
</style>
